<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use App\Services\Tools\MapService;

class GiftWallRepository
{
    const COLOR_LIST = [
            ['id' => 1, 'name' => '蓝色'],
            ['id' => 2, 'name' => '紫色'],
            ['id' => 3, 'name' => '粉红色'],
            ['id' => 4, 'name' => '绿色'],
            ['id' => 5, 'name' => '橙色']
        ];

    /**
     * 礼物兑换配置详情
     */
    public function exchangeConfigDetail(int $id)
    {
        $allGiftMap = MapService::allGiftMap();
        $detail = [];
        if ($id) {
            $detail = app('model_user_gift_wall_exchange_config')
                ->where('id', $id)
                ->first();
            if (empty($detail)) throw new BasicException(70101);
            $detail = $detail->toArray();
            $detail['content'] = json_decode($detail['content'], true);
        }
        $rewardList = app('model_reward')
            ->where('active', 1)
            ->select('id', 'name')
            ->orderBy('id', 'desc')
            ->get()->toArray();

        return [
            'all_gift_list' => array_values($allGiftMap),
            'detail' => $detail,
            'reward_list' => $rewardList
        ];
    }

    /**
     * 礼物兑换配置保存
     */
    public function exchangeConfigSave(array $params)
    {
        $data = [
            'title' => $params['title'] ?? '',
            'content' => $params['content'] ?? '',
            'reward_type' => 1,
            'reward_id' => $params['reward_id'],
            'reward_number' => $params['reward_number'],
            'status' => $params['status']
        ];
        if ($params['id']) {
            $detail = app('model_user_gift_wall_exchange_config')->where('id', $params['id'])->count();
            if (empty($detail)) throw new BasicException(70101);
            app('model_user_gift_wall_exchange_config')->where('id', $params['id'])->update($data);
            return [];
        }
        $data['add_time'] = date('Y-m-d H:i:s');
        app('model_user_gift_wall_exchange_config')->insert($data);
        return [];
    }

    /**
     * 礼物墙评分奖励配置删除
     */
    public function exchangeConfigDelete(array $ids)
    {
        $count = app('model_user_gift_wall_exchange_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->count();
        if (count($ids) != $count) throw new BasicException(70102);
        app('model_user_gift_wall_exchange_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->update([
                'status' => 2
            ]);
        return [];
    }

    /**
     * 礼物墙评分奖励配置列表
     */
    public function exchangeConfigList(int $page, int $perPage)
    {
        $list = app('model_user_gift_wall_exchange_config')
            ->whereIn('status', [0,1])
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page)->toArray();
        if (!empty($list['data'])) {
            $rewardMap = MapService::allRewardMap();
            $allGiftMap = MapService::allGiftMap();
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['reward_picture'] = $rewardMap[$value['reward_id']]['picture'] ?? '';
                $list['data'][$key]['reward_name'] = $rewardMap[$value['reward_id']]['name'] ?? '';
                $contentArray = json_decode($value['content'], true);
                foreach ($contentArray as $k => $v) {
                    $contentArray[$k]['gift_name'] = $allGiftMap[$v['gift_id']]['name'] ?? '';
                    $contentArray[$k]['gift_picture'] = $allGiftMap[$v['gift_id']]['picture'] ?? '';
                    $contentArray[$k]['gift_price'] = $allGiftMap[$v['gift_id']]['price'] ?? 0;
                }
                $list['data'][$key]['content'] = $contentArray;
            }
        }

        $stime = app('redis')->get('gift_wall:exchange_stime');
        $etime = app('redis')->get('gift_wall:exchange_etime');

        return [
            'total' => $list['total'],
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'stime' => $stime,
            'etime' => $etime,
            'list' => $list['data']
        ];
    }

    /**
     * 礼物墙评分奖励配置详情
     */
    public function scorePrizeConfigDetail(int $id)
    {
        $giftTypeList = app('model_gift_type')
            //->whereBetween('id', [23,25])
            ->where('parent_id', 0)
            ->select('id','name')
            ->get()->toArray();
        $colorList = self::COLOR_LIST;
        $rewardList = app('model_reward')
            ->where('active', 1)
            ->select('id', 'name')
            ->orderBy('id', 'desc')
            ->get()->toArray();

        $detail = [];
        if ($id) {
            $detail = app('model_user_gift_wall_score_prize_config')
                ->where('id', $id)
                ->first();
            if (empty($detail)) throw new BasicException(70101);
            $detail = $detail->toArray();
        }
        $statusList = [
            ['id' => 0, 'name' => '不展示'],
            ['id' => 1, 'name' => '展示'],
            ['id' => 2, 'name' => '已删除']
        ];

        return [
            'detail' => $detail,
            'gift_type_list' => $giftTypeList,
            'color_list' => $colorList,
            'reward_list' => $rewardList,
            'status_list' => $statusList
        ];
    }

    /**
     * 礼物墙评分奖励配置保存
     */
    public function scorePrizeConfigSave(array $params)
    {
        $data = [
            'title' => $params['title'],
            'target_score' => $params['target_score'],
            'extra_type' => $params['extra_type'],
            'extra_score' => $params['extra_score'],
            'color' => $params['color'],
            'reward_type' => 1,
            'reward_id' => $params['reward_id'],
            'reward_number' => $params['reward_number'],
            'status' => $params['status']
        ];
        if ($params['id']) {
            $detail = app('model_user_gift_wall_score_prize_config')->where('id', $params['id'])->count();
            if (empty($detail)) throw new BasicException(70101);
            app('model_user_gift_wall_score_prize_config')->where('id', $params['id'])->update($data);
            return [];
        }
        $data['add_time'] = date('Y-m-d H:i:s');
        app('model_user_gift_wall_score_prize_config')->insert($data);
        return [];
    }

    /**
     * 礼物墙评分奖励配置删除
     */
    public function scorePrizeConfigDelete(array $ids)
    {
        $count = app('model_user_gift_wall_score_prize_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->count();
        if (count($ids) != $count) throw new BasicException(70102);
        app('model_user_gift_wall_score_prize_config')
            ->whereIn('id', $ids)
            ->whereIn('status', [0,1])
            ->update([
                'status' => 2
            ]);
        return [];
    }

    /**
     * 礼物墙评分奖励配置列表
     */
    public function scorePrizeConfigList(int $page, int $perPage)
    {
        $list = app('model_user_gift_wall_score_prize_config')
            ->whereIn('status', [0,1])
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page)->toArray();
        if (!empty($list['data'])) {
            $rewardMap = MapService::allRewardMap();
            $colorMap = array_column(self::COLOR_LIST, null, 'id');
            $giftTypeMap = MapService::giftTypeMap();
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['reward_picture'] = $rewardMap[$value['reward_id']]['picture'] ?? '';
                $list['data'][$key]['reward_name'] = $rewardMap[$value['reward_id']]['name'] ?? '';
                $list['data'][$key]['extra_type_name'] = $giftTypeMap[$value['extra_type']]['name'] ?? '';
                $list['data'][$key]['color_name'] = $colorMap[$value['color']]['name'] ?? '';
            }
        }

        return [
            'total' => $list['total'],
            'current_page' => $list['current_page'] ?? 1,
            'last_page' => $list['last_page'] ?? 1,
            'list' => $list['data']
        ];
    }
}
?>